//
//  KZVocabularyFailureReviewView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/9/6.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZVocabularyFailureReviewView.h"

// view
#import "DicMainView.h"

// tool
#import "BKAudioPlayer.h"                   // 音视频播放

@implementation KZVocabularyFailureReviewView
{
    dispatch_block_t        _playAudioTask;
    
    UIImageView             *_letterBgView;             // 单词背景视图，一只企鹅
    UILabel                 *_letterLabel;              // 单词
    UIButton                *_playVoiceButton;          // 播放音频的按钮
    UIButton                *_coursewareButton;         // 课件按钮：显示课件解析
}

- (void)dealloc {
    
    NSLog(@"销毁了");
}

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _letterBgView = [UIImageView new];
    _letterBgView.image = [UIImage imageNamed:@"kz-音标鸽子背景"];
    
    [self addSubview:_letterBgView];
    [_letterBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@50);
        make.centerX.equalTo(@0);
        if (iPhone5S) {
            make.height.equalTo(@160);
            make.width.equalTo(_letterBgView.mas_height).multipliedBy(525.0/431);
        }
    }];
    
    _letterLabel = [[UILabel alloc] init];
    _letterLabel.adjustsFontSizeToFitWidth = true;
    _letterLabel.numberOfLines = 1;
    _letterLabel.text = @"?";
    _letterLabel.font = B_FONT(45);
    _letterLabel.textAlignment = NSTextAlignmentCenter;
    _letterLabel.textColor = [UIColor whiteColor];
    [_letterBgView addSubview:_letterLabel];
    [_letterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_letterBgView).offset(2);
        make.centerX.equalTo(@0);
        make.width.equalTo(_letterBgView).multipliedBy(350.0/525);
    }];
    
    _playVoiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_playVoiceButton setImage:[UIImage imageNamed:@"kz-发音图标"] forState:UIControlStateNormal];
    [_playVoiceButton addTarget:self action:@selector(playAudio) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_playVoiceButton];
    [_playVoiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_letterBgView).offset(10);
        make.width.height.mas_equalTo(W(44));
        make.right.equalTo(self.mas_centerX).offset(-20);
    }];

    _coursewareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_coursewareButton setImage:[UIImage imageNamed:@"kz-解析课件"] forState:UIControlStateNormal];
    [_coursewareButton addTarget:self action:@selector(analyze:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_coursewareButton];
    [_coursewareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_letterBgView).offset(10);
        make.width.height.mas_equalTo(W(44));
        make.left.equalTo(self.mas_centerX).offset(20);
    }];
}


#pragma mark - setter
- (void)setModel:(KZVocabularyCurrentPackageDetailModel *)model {
    
    _model = model;

    _letterLabel.text = _model.Vocabulary;
}

#pragma mark - 事件
// 播放音频
- (void)playAudio {
    
    // 正在播放中，不播放
    if ([BKAudioPlayer sharedMPlayer].player.status == MAudioStatePlaying) {
        [[BKAudioPlayer sharedMPlayer] playerPaused];
        [[BKAudioPlayer sharedMPlayer] closePlayer];
    }
    
    NSString *url = _model.VocabularyAudioPath;
    [[BKAudioPlayer sharedMPlayer] playerUrl:[NSURL URLWithString:url] seekTotime:0 rateValue:1];
    [[BKAudioPlayer sharedMPlayer] playerPlay];
}
// 课件解析
- (void)analyze:(UIButton *)button {
    
    DicMainView *main = [[DicMainView alloc] initWithTestType:DictTestType_Word isShowRecord:YES];
    main.dictUrl = [NSString stringWithFormat:@"%@api/ZXYYTS/W_ZXYYTS_GetCoursewareByCode",KGetUserDefaults(@"SystemIP")];
    main.dictParam = @{@"stageNo":KGetUserDefaults(@"TestLayer"),
                       @"schoolId":DR_UserInfo.SchoolID,
                       @"stuId":DR_UserInfo.UserID,
                       @"knowledge": _model.VocabularyID,
                       @"levelCode": KGetUserDefaults(@"TestLayer")
                       };
    [main popAnimated:YES];
}

#pragma mark - 辅助方法
// 开起一个延时任务
- (dispatch_block_t)beginBlockTask:(void(^)(void))block after:(NSTimeInterval)after {
    
    dispatch_block_t task = dispatch_block_create(DISPATCH_BLOCK_DETACHED, block);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(after * NSEC_PER_SEC)), dispatch_get_main_queue(), task);
    return task;
}
@end
